package com.woniu.user.service.impl;

import com.woniu.common.entity.ApplyForTable;
import com.woniu.common.entity.Cancellation;
import com.woniu.common.entity.Dog;
import com.woniu.common.entity.User;
import com.woniu.user.entity.Perm;
import com.woniu.user.mapper.UserMapper;
import com.woniu.user.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public User findUserByAccont(String account) {
        if(redisTemplate.hasKey(account)){
            //2.有直接返回
            System.out.println("redis装有数据,直接返回");
            return (User) redisTemplate.opsForValue().get(account);
        }
        //
        User user = userMapper.findUserByAccont(account);
        //没有数据,走数据库
        if(user!=null){
            System.out.println("这是走了数据库的");
            redisTemplate.opsForValue().set(account, user);
            return user;
        }
        return null;
    }

    @Override
    public boolean updateInfo(User user) {
        return userMapper.updateInfo(user);
    }

    @Override
    public List<Perm> queryAllMenu(int uid) {
        //查出所有的rid
        List<Integer> listrid = userMapper.findByRid(uid);
        //这个保存所有的权限
        List listAllPerms = new ArrayList();
        //查出所有的pid
        List<Integer> listPid = new LinkedList<>();
        //查出当前pid下的id
        List<Integer> list = new ArrayList();
        List listPremsPid = new LinkedList<>();
        for (int i = 0; i < listrid.size(); i++) {
            listPid = userMapper.findBypid(listrid.get(i));
            for (int j = 0; j < listPid.size(); j++) {
                list = userMapper.findPermsid(listPid.get(j));
                for (int p = 0; p < list.size(); p++) {
                    //这里需要重新new,不然会覆盖之前添加的
                    Perm prems = new Perm();
                    prems = findPerms(list.get(p), prems);
                    listAllPerms.add(prems);
                }
            }
        }
        System.out.println(listAllPerms);
        return listAllPerms;
    }

    //这个findperms里面传的id就是主键id
    Perm findPerms(int id, Perm perm) {
        perm.setChildren(new ArrayList<>());
        //通过pid查询出id.....Select("select id,name,pid from perms where pid = #{pid}")
        Perm permone = userMapper.findPerm(id);
        List<Integer> listPids = userMapper.findAllPermsPid(id);
        if (listPids.size() != 0) {
            for (int i = 0; i < listPids.size(); i++) {
                perm.setMenuname(permone.getMenuname());
                perm.setMenuid(permone.getMenuid());
                perm.setPid(permone.getPid());
                perm.setUrl(permone.getUrl());
                perm.setIcon(permone.getIcon());
                perm.getChildren().add(findPerms(listPids.get(i), new Perm()));
            }
        } else {
            return permone;
        }
        return perm;
    }

    @Override
    public boolean insertAccount(String account) {
        return userMapper.insertAccount(account);
    }

    @Override
    public boolean addPersonalPerm(int uid) {
        return userMapper.addPersonalPerm(uid);
    }

    @Override
    public boolean addHospitalPerm(int uid) {
        return userMapper.addHospitalPerm(uid);
    }

    @Override
    public boolean addPolicePerm(int uid) {
        return userMapper.addPolicePerm(uid);
    }

    @Override
    public User findUser(int id) {
        return userMapper.findUser(id);
    }

    @Override
    public boolean InsertApplyFor(ApplyForTable applyForTable) {
        return userMapper.InsertApplyFor(applyForTable);
    }

    @Override
    public List<ApplyForTable> raiseDog(int id) {
        return userMapper.raiseDog(id);
    }

    @Override
    public List<Dog> findNormalPet(int id) {
        return userMapper.findNormalPet(id);
    }

    @Override
    public boolean cancelDog(Cancellation cancellation) {
        if(userMapper.cancelDog(cancellation)){
            //修改当前用户下这只狗狗的状态
            userMapper.changeDog(cancellation.getPet_id());
            //插入成功后将id发到消息队列,然后设置2天后过期,在过期后修改
            rabbitTemplate.convertAndSend("userexchange","user",cancellation.getId());
            System.out.println(cancellation);
            redisTemplate.opsForHash().put("cancelDog",""+cancellation.getId()+"",cancellation);
            return true;
        }
        return false;
    }

    @Override
    public List<Cancellation> selectCancelDog() {
        return userMapper.selectCancelDog();
    }

    @Override
    public boolean manageCancelDog(Cancellation cancellation) {
        //先修改审批表里面的状态
        if(userMapper.manageCancelDog(cancellation)){
            //如果是同意注销,在修改用户表里面的狗狗状态
            if(cancellation.getAudit()==1){
                userMapper.sureCancelDog(cancellation.getPet_id());
            }
            //不同意注销,将状态修改为0,即正常状态
            else if(cancellation.getAudit()==2){
                userMapper.noSureCancelDog(cancellation.getPet_id());
            }
            if(redisTemplate.opsForHash().hasKey("cancelDog",""+cancellation.getId()+"")){
                redisTemplate.opsForHash().delete("cancelDog",""+cancellation.getId()+"");
            }
            return true;
        }
        return false;
    }
}
